﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.EventBroker;
using StockEODUI.ManageImports.Constants;
using StockEODUI.Infrastructure.Interface;
using System.IO;
using SQLManager;
using StockEOD;
namespace StockEODUI.ManageImports.Services
{
    [Service]
    public class EODDataService
    {

        public EODDataService()
        {
            Importer.OnStatusMessage += new StatusMessageDelgate(Importer_OnStatusMessage);
            Importer.OnPercentCompleteChange += new PercentDoneDelegate(Importer_OnPercentCompleteChange);
        }

        void Importer_OnPercentCompleteChange(float percent)
        {
            fireOnPercentCompleteChanged(percent);
        }


        void Importer_OnStatusMessage(string message)
        {
            fireServiceMessage(message);
        }
        

        public void ImportMarketData(DirectoryInfo dir, MarketContext args)
        {

            StockEOD.Importer.ImportTickers(dir, args.MarketData.Code, false);
            if (OnProcessComplete != null)
            {
                OnProcessComplete(this, new EventArgs<string>("ImportMarketData"));
            }
        }
        public void DeleteMarketData(MarketContext args)
        {
            int countFund = 0;
            int countStat = 0;
            int countDailyDetail = 0;
            int countMarketDays = 0;
            int countTickers = 0;

            fireServiceMessage(string.Format("Marking Stock Stats, Daily Detail and MarketDays in {0} for deletion...", args.MarketData.Code));
            float ttlMD = args.MarketData.MarketDays.Count() + args.MarketData.Tickers.Count();
            ttlMD = ttlMD + ttlMD * .35f; // fudge factor to allow for time for the update to occur.
            float  posCnt = 0;
            if (ttlMD > 0)
            {
                fireOnPercentCompleteChanged(posCnt / ttlMD);
            }
            foreach (MarketDay md in args.MarketData.MarketDays)
            {
                foreach (DailyDetail dd in md.DailyDetails)
                {
                    foreach (DailyStatsPerStock dsps in dd.DailyStatsPerStocks)
                    {

                        args.Context.DailyStatsPerStocks.DeleteOnSubmit(dsps);
                        countStat++;
                    }
                    countDailyDetail++;
                    args.Context.DailyDetails.DeleteOnSubmit(dd);
                }
                posCnt++;
                fireOnPercentCompleteChanged(posCnt / ttlMD);
                countMarketDays++;
                args.Context.MarketDays.DeleteOnSubmit(md);
            }
            fireServiceMessage(string.Format("Marking Tickers and Fundamentals in {0} for deletion...", args.MarketData.Code));
            foreach (Ticker t in args.MarketData.Tickers)
            {

                foreach (Fundamental f in t.Fundamentals)
                {
                    args.Context.Fundamentals.DeleteOnSubmit(f);
                    countFund++;
                }
                args.Context.Tickers.DeleteOnSubmit(t);
                posCnt++;
                fireOnPercentCompleteChanged(posCnt / ttlMD);
                countTickers++;
            }
            foreach (Sector s in args.MarketData.Sectors)
            {
                s.Industries.ToList().ForEach(i => args.Context.Industries.DeleteOnSubmit(i));
                args.Context.Sectors.DeleteOnSubmit(s);
            }
            args.Context.Markets.DeleteOnSubmit(args.MarketData);
            fireServiceMessage(string.Format("Updating database, removing :"));
            fireServiceMessage(string.Format("Stock Stats : {0} \t Fundementals : {1}", countStat, countFund));
            fireServiceMessage(string.Format("DailyDetail : {0} \t MarketDays : {1}", countDailyDetail, countMarketDays));
            fireServiceMessage(string.Format("Tickers : {0} \t ", countTickers));
            args.Context.SubmitChanges();
            fireOnPercentCompleteChanged(100);
            fireServiceMessage("Delete Complete.");
            if (OnProcessComplete != null)
            {
                OnProcessComplete(this, new EventArgs<string>("DeleteMarketData"));
            }
            
        }
        private void fireServiceMessage(string message)
        {
            if(OnServiceMessage!=null)
            {
                OnServiceMessage(this, new EventArgs<string>(message));
            }
        }
        private void fireOnPercentCompleteChanged(float perDone)
        {
            if (OnPercentCompleteChanged != null)
            {
                OnPercentCompleteChanged(this, new EventArgs<float>(perDone));
            }
        }
        [EventPublication(EventTopicNames.SET_MESSAGE, PublicationScope.Global)]
        public event EventHandler<EventArgs<string>> OnServiceMessage;
        [EventPublication(EventTopicNames.PROCESS_COMPLETE, PublicationScope.Global)]
        public event EventHandler<EventArgs<string>> OnProcessComplete;
        [EventPublication(EventTopicNames.PERCENT_COMPLETE_CHANGED, PublicationScope.Global)]
        public event EventHandler<EventArgs<float>> OnPercentCompleteChanged;
       
    }
    public class MarketContext
    {
        public AppDataDataContext Context { get; set; }
        public Market MarketData{get;set;}
    }
}
    

